package gs.gui;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

public class SpellStatPanel extends JPanel {

	AdminWindow parent;
	private ConnectionController connection;
	DefaultListModel itemModel;
	JList itemList;
	JScrollPane itemScrollPane;
	JTextField nameField;
	JTextField typeField;
	JTextField damageField;
	JTextField costField;
	JTextField rangeField;
	
	SpellStatPanel(ConnectionController c, AdminWindow win)
	{
		super();
		connection = c;
		parent = win;
		
		this.setLayout(new GridLayout(1,2));
		
		JPanel choicePane = new JPanel(new BorderLayout());
		JPanel statPane = new JPanel(new GridLayout(9,2));
		
		choicePane.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Wyb�r"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
		
		statPane.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Statystyki"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
		
		itemModel = new DefaultListModel();
		
		itemList = new JList(itemModel);
		itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        itemList.setSelectedIndex(0);
        itemList.setVisibleRowCount(5);
        itemScrollPane = new JScrollPane(itemList);
        
        MouseListener accListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent mouseEvent) {
					getSpellData();
				}
		};
		itemList.addMouseListener(accListener);
        
        nameField = new JTextField(" ");
        nameField.setEditable(false);
        typeField = new JTextField(" ");
        typeField.setEditable(false);
        damageField = new JTextField(" ");
        damageField.setEditable(false);
        costField = new JTextField(" ");
        costField.setEditable(false);
        rangeField = new JTextField(" ");
        rangeField.setEditable(false);
        
        getSpellList();
        
        choicePane.add(itemScrollPane, BorderLayout.CENTER);
        statPane.add(new JLabel("Nazwa:"));
        statPane.add(nameField);
        statPane.add(new JLabel("Typ:"));
        statPane.add(typeField);
        statPane.add(new JLabel("Obrazenia:"));
        statPane.add(damageField);
        statPane.add(new JLabel("Koszt:"));
        statPane.add(costField);
        statPane.add(new JLabel("Zasieg:"));
        statPane.add(rangeField);
        
        add(choicePane);
        add(statPane);
	}
	
	void getSpellData()
	{
		ArrayList<String> a;
		String name = (String) itemList.getSelectedValue();
		a = connection.getSpellData(name);
		nameField.setText(a.get(0));
		typeField.setText(a.get(1));
		damageField.setText(a.get(2));
		costField.setText(a.get(3));
		rangeField.setText(a.get(4));
	}
	
	void getSpellList()
	{
		ArrayList<String> a;
		
 		a = connection.getSpellList();
		itemModel.clear();
		for(String s : a)
		{
			itemModel.addElement(s);
		}
	}

	public void refresh() {
		getSpellList();
		
	}

}
